package cn.edu.swu;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ScDao {
    public void add(Sc sc) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into sc(stuid,cid,score) values(?,?,?)";
        queryRunner.update(sql,sc.getStuId(),sc.getcId(),sc.getScore());
    }
    public void delete(Integer scId) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from sc where id=?";
        queryRunner.update(sql,scId);
    }
    public void update(Sc sc) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "update sc set score=? where scid=?";
        queryRunner.update(sql,sc.getScore(),sc.getScId());
    }
    public List<Sc> list(Sc sc) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from sc";
        List<Sc> list =queryRunner.query(sql,new BeanListHandler<>(Sc.class));
        return list;
    }
    public Sc findById(Integer scId) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from sc where scid=?";
        Sc sc =queryRunner.query(sql,new BeanHandler<>(Sc.class),scId);
        return sc;
    }
}
